package edu.buaa.act.travel.m.page;

import edu.buaa.act.travel.m.Config;
import edu.buaa.act.travel.m.Constants;
import edu.buaa.act.travel.m.R;
import edu.buaa.act.travel.m.service.DataService;
import edu.buaa.act.travel.m.utils.Utils;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {
	
	private static final int MENU_LOCATION = 101;
	
	private EditText mEditUser;
	private EditText mEditPass;
	
	private Handler mHandler = new Handler() {
		
		@Override
		public void handleMessage(final Message msg) {
			dismissDialog(DIALOG_WAIT);
			switch(msg.what) {
			case Constants.MESSAGE_GET_DATA_SUCCESS:
				forward((String) msg.obj);
				break;
			case Constants.MESSAGE_GET_DATA_FAIL:
				break;
			}
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        
        mEditUser = (EditText) findViewById(R.id.edit_username);
        mEditPass = (EditText) findViewById(R.id.edit_password);
        
        setupRightPanelButton(R.string.title_login, new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				login();
			}
		});
        setLeftPanelButtonVisibility(View.GONE);
    }
    
    private void login() {
    	final String user = mEditUser.getText().toString();
    	final String pass = mEditPass.getText().toString();
    	
    	if(Utils.isEmpty(user) || Utils.isEmpty(pass)) {
    		Toast.makeText(this, "Please input username and password", Toast.LENGTH_LONG).show();
    		return;
    	}
    	
    	showDialog(DIALOG_WAIT);
    	DataService.get(Constants.URL_LOGIN, 
    			mHandler, 
    			Constants.MESSAGE_GET_DATA_SUCCESS, 
    			Constants.MESSAGE_GET_DATA_FAIL);
    }
    
    private void forward(final String result) {
    	
    	final Intent intent = new Intent(this, TripListActivity.class);
    	//final Intent intent = new Intent(this,SurroundsActivity.class);
    	intent.putExtra(Constants.EXTRA_RESULT, result);
    	startActivity(intent);
    	finish();
    }
    
    @Override    
	public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(Menu.NONE, MENU_LOCATION, 1, "Set Location");
    	return true;
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {        
		case MENU_LOCATION:
			final View view = getLayoutInflater().inflate(R.layout.dialog_set_location, null);
			final EditText lat = (EditText) view.findViewById(R.id.edit_lat);
			final EditText lng = (EditText) view.findViewById(R.id.edit_lng);
			new AlertDialog.Builder(this).setTitle("Set Location").setView(view)
				.setPositiveButton("Set Location", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						Config.LAT = lat.getText().toString();
						Config.LNG = lng.getText().toString();
					}
				}).create().show();
			break;
    	}
    	return super.onOptionsItemSelected(item);
    }
}